//
//  YKSearchDataByRegionViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/1.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKSearchDataByRegionViewController.h"
#import "YKDataHeaderView.h"
#import "YKMultiSelectDropdownView.h"
#import "YKSaleModel.h"
#import "YKClientListHeaderView.h"
#import "YKSaleItem.h"
#import "YKRegionModel.h"
@interface YKSearchDataByRegionViewController ()
@property (nonatomic, strong) NSString *startTime;
@property (nonatomic, strong) NSString *endTime;
@property (nonatomic, strong) NSString *regionPath;
@property (nonatomic, strong) NSArray *regionSummaryDetail;
@property (nonatomic, strong) YKDataHeaderView *headerView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;

@property(nonatomic, strong) YKClientListHeaderView *listHeaderView;
@property(nonatomic, strong) YKMultiSelectDropdownView *multiSelectView1;
@property(nonatomic, strong) UILabel *rowLabel;
@property(nonatomic, strong) UIView *bottomView;
@end

@implementation YKSearchDataByRegionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"按区域查询";
    self.view.backgroundColor = [UIColor whiteColor];
    @weakify(self)
    YKDataHeaderView *headerView = [[YKDataHeaderView alloc]initWithType:YKDataHeaderViewTypeYearMonthDay];
    headerView.dateChangeBlock = ^(NSString *start, NSString *end) {
        self_weak_.startTime = start;
        self_weak_.endTime = end;
        [self_weak_ loadData];
    };
    self.headerView = headerView;
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
    }];

    self.multiSelectView1 = [[YKMultiSelectDropdownView alloc] initWithTitle:@"区域选择" rows:@[]
                                                                    sameLine:YES];
    self.multiSelectView1.selectHandler = ^(NSArray<YKDropdown> *values) {
        YKRegionModel *model = values.lastObject;
        self.regionPath = model.REGION_PATH;
        [self loadData];
    };
    
    YKRegionModel *allRegion = [YKRegionModel new];
    allRegion.REGION_NAME = @"全部";
    allRegion.REGION_PATH = @"";
    allRegion.REGION_ID = @"";
    
    
    if ([CommonDataManager sharedInstance].regions) {
        
        NSMutableArray *data = [[@[allRegion] arrayByAddingObjectsFromArray:[CommonDataManager sharedInstance].regions] mutableCopy];
        for (YKRegionModel *model in data) {
            if (![[model.subMap.firstObject name] isEqualToString:@"全部"]) {
                YKRegionModel *subAllRegion = [YKRegionModel new];
                subAllRegion.REGION_NAME = @"全部";
                subAllRegion.REGION_PATH = model.REGION_PATH;
                subAllRegion.REGION_ID = @"";
                subAllRegion.subMap = nil;
                model.subMap = (NSArray <YKRegionModel> *) [@[subAllRegion] arrayByAddingObjectsFromArray:model.subMap];
            }
        }
        
        self.multiSelectView1.modelData = data;
    }

    [self.view addSubview:self.multiSelectView1];
    
    [self.multiSelectView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom);
    }];
    self.multiSelectView1 = self.multiSelectView1;

    self.listHeaderView = [[YKClientListHeaderView alloc] initWithTitles:@[@"品种",@"销量",@"金额"] weights:@[@1,@1,@1]];
    self.listHeaderView.textFont = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.listHeaderView];
    [self.listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
    }];
    self.listHeaderView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];


    self.bottomView = [[UIView alloc]init];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];

    self.rowLabel = [[UILabel alloc]init];
    self.rowLabel.text = @"共0行";
    [self.bottomView addSubview:self.rowLabel];
    [self.rowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    self.rowLabel.tf_weight = 1;

    UILabel *leftLabel = [[UILabel alloc]init];
    leftLabel.text = @"销量：0";
    [self.bottomView addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    leftLabel.tf_weight = 2;
    UILabel *rightLabel = [[UILabel alloc]init];
    rightLabel.text = @"金额：0";
    [self.bottomView addSubview:rightLabel];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    rightLabel.tf_weight = 2;
    [@[self.rowLabel, leftLabel, rightLabel] mas_alignWeightlyAlongAxis:MASAxisTypeHorizontal fixedSpace:@0 leadingTo:self.view.mas_leading leadSpace:8 trailingTo:self.view.mas_trailing tailSpace:8];
    

    self.leftLabel = leftLabel;
    self.rightLabel = rightLabel;
    self.rowLabel.font = [UIFont boldSystemFontOfSize:16];
    leftLabel.font = [UIFont boldSystemFontOfSize:16];
    rightLabel.font = [UIFont boldSystemFontOfSize:16];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.listHeaderView.mas_bottom);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
    [self.view bringSubviewToFront:self.tableView];
    [self loadData];

    UIBarButtonItem *filterItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(onFilterItemTapped:)];
    self.navigationItem.rightBarButtonItem = filterItem;

}

- (void)onFilterItemTapped:(UIBarButtonItem *)item {
    if (item.tag == 0) {
        [UIView animateWithDuration:.3f animations:^{
            [self.listHeaderView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.equalTo(self.view);
                make.top.equalTo(self.multiSelectView1.mas_bottom);

            }];
            [self.view layoutIfNeeded];
        }];

        item.tag = 1;
    } else{
        item.tag = 0;
        [UIView animateWithDuration:.3f animations:^{
            [self.listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.equalTo(self.view);
                make.top.equalTo(self.view.mas_top);
            }];
            [self.view layoutIfNeeded];
        }];
    }

}

- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading inView:self.tableView];
    [TFHTTPMANAER POST:@"manage/regionSummaryDetail.htm" parameters:@{@"startDate":self.headerView.start, @"endDate": self.headerView.end, @"regionPath":self.multiSelectView1.selectedModels.lastObject?[self.multiSelectView1.selectedModels.lastObject REGION_PATH]:@""} success:^(id responseObject) {
        
        [self tf_showStateView:kTFViewStateNone inView:self.tableView];
        [self.tableViewManager removeAllSections];
        self.regionSummaryDetail = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"regionSummaryDetail"]];
        
        TFTableViewSection * section = [[TFTableViewSection alloc]init];
        section.headerHeight = .1f;
        section.footerHeight = .1f;
        [section addItemsFromArray:[self.regionSummaryDetail tf_mapUsingBlock:^id _Nullable(id  _Nonnull obj, NSInteger idx) {
            return [YKSaleItem itemWithModel:obj];
        }]];
        
        CGFloat sale = 0, gold = 0;
        for (YKSaleModel *model in self.regionSummaryDetail) {
            gold += model.SALES;
            sale += model.SALES_NUM.floatValue;
        }
        self.leftLabel.text = [NSString stringWithFormat:@"销量:%.0f", sale];
        self.rightLabel.text = [NSString stringWithFormat:@"金额:%.2f", gold];
        self.rowLabel.text = [NSString stringWithFormat:@"共%d行", self.regionSummaryDetail.count];

        if (section.items.count == 0) {
            [self tf_showStateView:kTFViewStateNoData inView:self.tableView];
        }
        [self.tableViewManager addSection:section];
        
        [self.tableView reloadData];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError inView:self.tableView];
    }];
}


@end
